package utils.fileHandler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import utils.utilTime.UtilTime;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static utils.jsonHandler.JsonReadWrite.readJsonFile;
import static utils.jsonHandler.JsonReadWrite.writeJsonFile;

public class FileHandler {
    private static JSONObject settingObject;

    /*private static long intervalTime;
    private static double transparency;
    private static double audio;
    private static String wallpaperJSON;
    private static String settingJSON;
    private static String groupInfoJSON;
    private static String hDVideoDirName;
    private static String preVideoDirName;
    private static String videoWallpaperPath;
    private static String hDImageDirName;
    private static String preImageDirName;
    private static String imageWallpaperPath;
    private static String isImageWallpaperChanged;
    private static String isVideoWallpaperChanged;
    private static JSONArray webViewUrl;*/

    private static String blackBackGroundImage;
    private static String sourcePath;
    private static String rootPath;

    static {
        settingObject = JSON.parseObject(readJsonFile("json/setting.json"));//读取setting.json文件，获得程序配置
    }
    public static String getRootPath(){return settingObject.getString("rootPath");}

    public static String getSourcePath(){return settingObject.getString("sourcePath");}

    public static String getBlackBackGroundImage(){return settingObject.getString("blackBackGroundImage");}

    public static long getIntervalTime(){return settingObject.getLong("intervalTime");}

    public static double getTransparency(){return settingObject.getDouble("transparency");}

    public static double getAudio(){return settingObject.getDouble("audio");}

    public static String getWallpaperJSON() {
        return settingObject.getString("wallpaperJSON");
    }

    public static String getSettingJSON() {
        return "json/setting.json";
    }

    public static String getGroupInfoJSON() {
        return settingObject.getString("groupInfoJSON");
    }

    public static String gethDVideoDirName() {
        return settingObject.getString("hDVideoDirName");
    }

    public static String getPreVideoDirName() {
        return settingObject.getString("preVideoDirName");
    }

    public static String getVideoWallpaperPath() {
        return settingObject.getString("videoWallpaperPath");
    }

    public static String gethDImageDirName() {
        return settingObject.getString("hDImageDirName");
    }

    public static String getPreImageDirName() {
        return settingObject.getString("preImageDirName");
    }

    public static String getImageWallpaperPath() {
        return settingObject.getString("imageWallpaperPath");
    }

    public static boolean getIsImageWallpaperChanged() {
        return settingObject.getBoolean("isImageWallpaperChanged");
    }

    public static boolean getIsVideoWallpaperChanged() {
        return settingObject.getBoolean("isVideoWallpaperChanged");
    }

    public static String gethDVideoDirAbsolutePath() {
        if (getIsVideoWallpaperChanged()) {
            return getVideoWallpaperPath() + File.separator + gethDVideoDirName();
        } else {
            File file = new File(getVideoWallpaperPath() + File.separator + gethDVideoDirName());
            return file.getAbsolutePath();
        }
    }

    public static String getPreVideoDirAbsolutePath() {
        if (getIsVideoWallpaperChanged()) {
            return getVideoWallpaperPath() + File.separator + getPreVideoDirName();
        } else {
            File file = new File(getVideoWallpaperPath() + File.separator + getPreVideoDirName());
            return file.getAbsolutePath();
        }
    }

    public static String gethDImageDirAbsolutePath() {
        if (getIsImageWallpaperChanged()) {
            return getImageWallpaperPath() + File.separator + gethDImageDirName();
        } else {
            File file = new File(getImageWallpaperPath() + File.separator + gethDImageDirName());
            return file.getAbsolutePath();
        }
    }

    public static String getPreImageDirAbsolutePath() {
        if (getIsImageWallpaperChanged()) {
            return getImageWallpaperPath() + File.separator + getPreImageDirName();
        } else {
            File file = new File(getImageWallpaperPath() + File.separator + getPreImageDirName());
            return file.getAbsolutePath();
        }
    }

    public static List<String> getWebViewUrl() {
        List<String> webViewUrls = new ArrayList<>();
        for (Object viewUrl : settingObject.getJSONArray("webViewUrl")) {
            webViewUrls.add((String) viewUrl);
        }
        return webViewUrls;
    }
    public static void setSourcePath(String sourcePath){settingObject.put("sourcePath",sourcePath);}

    public static void setIntervalTime(long intervalTime){settingObject.put("intervalTime",intervalTime);}

    public static void setTransparency(double transparency){settingObject.put("transparency",transparency);}

    public static void setAudio(double audio){settingObject.put("audio",audio);}

    public static void setWallpaperJSON(String wallpaperJSON) {
        settingObject.put("wallpaperJSON" , wallpaperJSON);
    }

    public static void setGroupInfoJSON(String groupInfoJSON) {
        settingObject.put("groupInfoJSON" , groupInfoJSON);
    }

    public static void sethDVideoDirName(String hDVideoDirName) {
        settingObject.put("hDVideoDirName" , hDVideoDirName);
    }

    public static void setPreVideoDirName(String preVideoDirName) {
        settingObject.put("preVideoDirName" , preVideoDirName);
    }

    public static void setVideoWallpaperPath(String videoWallpaperPath) {
        settingObject.put("videoWallpaperPath" , videoWallpaperPath);
    }

    public static void sethDImageDirName(String hDImageDirName) {
        settingObject.put("hDImageDirName" , hDImageDirName);
    }

    public static void setPreImageDirName(String preImageDirName) {
        settingObject.put("preImageDirName" , preImageDirName);
    }

    public static void setImageWallpaperPath(String imageWallpaperPath) {
        settingObject.put("imageWallpaperPath" , imageWallpaperPath);
    }

    public static void setIsImageWallpaperChanged(Boolean isImageWallpaperChanged) {
        settingObject.put("isImageWallpaperChanged" , isImageWallpaperChanged);
    }

    public static void setIsVideoWallpaperChanged(Boolean isVideoWallpaperChanged) {
        settingObject.put("isVideoWallpaperChanged" , isVideoWallpaperChanged);
    }

    public static void setWebViewUrl(List<String> webViewUrl) {
        settingObject.put("webViewUrl" , webViewUrl);
    }


    public synchronized static boolean exit() {
        return save();
    }

    public synchronized static boolean save() {
        writeJsonFile(FileHandler.getSettingJSON() , settingObject.toString());
        System.out.println("Log : save " + FileHandler.getSettingJSON() + " success" + UtilTime.getStringTimeNow());
        return true;
    }

    public static String getNameWithoutType(String str) {
        String[] split = str.split("\\.");
        for (int i = 1; i < split.length - 1; i++) {
            split[0] += "." + split[i];
        }
        return split[0];
    }

    public static String getFileType(String str) {
        String[] split = str.split("\\.");
        return split[split.length - 1];
    }

    public static boolean deleteFile(String path) {
        File file = new File(path);
        if (file.exists()) {
            if (file.isDirectory()) {
                boolean delete = file.delete();
                System.out.println("Log : Delete dir " + path);
                return delete;
            } else {
                boolean delete = file.delete();
                System.out.println("Log : Delete file " + path);
                return delete;
            }
        } else {
            System.out.println("Error ：Delete fail, " + path + " no found");
            return true;
        }
    }

    public static boolean moveFile(String oldPath, String newPath) {
        File oldFile = new File(oldPath);
        File newFile = new File(newPath);
        if (oldFile.exists()) {
            if (newFile.getParentFile().exists()) {

            } else {
                System.out.println("Error : Directory " + newFile.getParent() + " no found");
            }
        } else {
            System.out.println("Error : 原文件 " + oldPath + " 不存在");
        }
        return false;
    }
}
